@file:OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterial3Api::class)

package com.compose.universally.components

import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FabPosition
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.IconButtonColors
import androidx.compose.material3.IconButtonDefaults
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.ScaffoldDefaults
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarColors
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.material3.contentColorFor
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import com.compose.universally.BuilderComposable
import com.compose.universally.BuilderComposableT
import com.compose.universally.BuilderComposableV
import com.compose.universally.Callback

@Composable
fun BaseScaffold(
    titleText: String? = null,
    enableNavigationIcon: Boolean = true,
    onNavigationClick: Callback? = null,
    // TopAppBar
    title: BuilderComposable? = null,
    navigationIcon: BuilderComposable? = null,
    actions: BuilderComposableT<RowScope>? = null,
    windowInsets: WindowInsets = TopAppBarDefaults.windowInsets,
    colors: TopAppBarColors = TopAppBarDefaults.topAppBarColors(),
    scrollBehavior: TopAppBarScrollBehavior? = null,
    topBarModifier: Modifier = Modifier,
    // Scaffold
    modifier: Modifier = Modifier,
    topBar: BuilderComposable = {
        AppBar(
            titleText = titleText,
            enableNavigationIcon = enableNavigationIcon,
            onNavigationClick = onNavigationClick,
            title = title,
            navigationIcon = navigationIcon,
            actions = actions,
            windowInsets = windowInsets,
            colors = colors,
            scrollBehavior = scrollBehavior,
            modifier = topBarModifier
        )
    },
    bottomBar: BuilderComposable = {},
    snackBarHost: BuilderComposable = {},
    floatingActionButton: BuilderComposable = {},
    floatingActionButtonPosition: FabPosition = FabPosition.End,
    containerColor: Color = MaterialTheme.colorScheme.background,
    contentColor: Color = contentColorFor(containerColor),
    contentWindowInsets: WindowInsets = ScaffoldDefaults.contentWindowInsets,
    // 是否默认 Padding
    isPadding: Boolean = true,
    // 是否填充剩余空间
    isFillMax: Boolean = true,
    content: BuilderComposableV<PaddingValues>,
) {
    Scaffold(modifier = modifier,
        topBar = topBar,
        bottomBar = bottomBar,
        snackbarHost = snackBarHost,
        floatingActionButton = floatingActionButton,
        floatingActionButtonPosition = floatingActionButtonPosition,
        containerColor = containerColor,
        contentColor = contentColor,
        contentWindowInsets = contentWindowInsets,
        content = {
            if (isPadding || isFillMax) {
                Box(
                    modifier = Modifier
                        .padding(it)
                        .fillMaxSize()
                ) {
                    content(it)
                }
            } else {
                content(it)
            }
        })
}

@Composable
fun AppBar(
    titleText: String? = null,
    enableNavigationIcon: Boolean = true,
    onNavigationClick: Callback? = null,
    // TopAppBar
    title: BuilderComposable? = null,
    navigationIcon: BuilderComposable? = null,
    actions: BuilderComposableT<RowScope>? = null,
    windowInsets: WindowInsets = TopAppBarDefaults.windowInsets,
    colors: TopAppBarColors = TopAppBarDefaults.topAppBarColors(),
    scrollBehavior: TopAppBarScrollBehavior? = null,
    modifier: Modifier = Modifier,
) {
    if (title != null || enableNavigationIcon || titleText != null || actions != null) TopAppBar(
        modifier = modifier,
        navigationIcon = navigationIcon ?: {
            if (enableNavigationIcon) BackButton(onClick = onNavigationClick)
        },
        actions = actions ?: {},
        windowInsets = windowInsets,
        colors = colors,
        scrollBehavior = scrollBehavior,
        title = title ?: {
            if (titleText != null) TextLager(titleText)
        })
}

@Composable
fun BackButton(
    // IconButton
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    colors: IconButtonColors = IconButtonDefaults.iconButtonColors(),
    interactionSource: MutableInteractionSource? = null,
    onClick: Callback? = null,
    // Icon
    imageVector: ImageVector = Icons.AutoMirrored.Filled.ArrowBack,
    contentDescription: String? = null,
    iconModifier: Modifier = Modifier,
    tint: Color = LocalContentColor.current
) {
    IconButton(modifier = modifier,
        enabled = enabled,
        colors = colors,
        interactionSource = interactionSource,
        onClick = {
            onClick?.invoke() ?: popBackStack()
        },
        content = {
            Icon(
                imageVector,
                contentDescription = contentDescription,
                tint = tint,
                modifier = iconModifier
            )
        })
}